/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.insurenceStep1.Controller;

import com.Auto.Common.InsuranceUpdate.Hibernate.Insuranceupdate;
import com.insurenceStep1.Model.insurenceStep1_Model;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Yogesh
 */
public class insurenceStep1_Controller extends org.apache.struts.action.Action {
    
    /* forward name="success" path="" */
    private static final String SUCCESS = "insurenceStep1";
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        com.insurenceStep1.Model.insurenceStep1_Model model=(insurenceStep1_Model) form;

        String insuranceVehicleName=model.getInsuranceVehicleName();
        String insuranceManufacturingCompanyName=model.getInsuranceManufacturingCompanyName();
        String insuranceVehicleChassisNumber=model.getInsuranceVehicleChassisNumber();
        String insuranceVehicleRCBook=model.getInsuranceVehicleRCBook();
        String insuranceVehicleType=model.getInsuranceVehicleType();

        String insuranceDriverLicense="";
        String insuranceIdProof="";
        String insuranceAddressProof="";
        String insurancePhotos="";
        String insuranceSalarySleep="";
        String insuranceFirstCheck="";
        String insuranceSecondCheck="";
        String insuranceThirdCheck="";
        String insuranceAdharCard="";
        
        String activationCode=model.getActivationCode();

        org.hibernate.SessionFactory sessionFactory=com.Auto.Common.InsuranceUpdate.Hibernate.InsuranceUpdate_HibernateUtil.getSessionFactory();
        org.hibernate.Session Opensession=sessionFactory.openSession();
        org.hibernate.Transaction transaction=Opensession.beginTransaction();



         String str="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                StringBuffer sb = new StringBuffer(5);

             for (int i=0;  i<5;  i++) {


                 int ndx = (int)(Math.random()*str.length());

                 sb.append(str.charAt(ndx));

             }



      
                //VVV Imp
                //insuranceIdProof Refer To InsuranceUniqueCode
                //
                insuranceIdProof=sb+"";

                 //VVV Imp
                //insuranceDriverLicense Refer To InsuranceApproval
                //
                insuranceDriverLicense="0"+"";




          String idFromUserInformation=model.getIdFromUserInformation();
        com.Auto.Common.InsuranceUpdate.Hibernate.Insuranceupdate insuranceupdate=new Insuranceupdate(idFromUserInformation, insuranceVehicleName, insuranceManufacturingCompanyName, insuranceVehicleChassisNumber, insuranceVehicleRCBook, insuranceVehicleType, insuranceDriverLicense, insuranceIdProof, insuranceAddressProof, insurancePhotos, insuranceSalarySleep, insuranceFirstCheck, insuranceSecondCheck, insuranceThirdCheck, insuranceAdharCard);
        Opensession.save(insuranceupdate);
        transaction.commit();


         java.io.File file = new java.io.File("VehicleHai/"+activationCode+"/Insurance/"+insuranceIdProof);
	        if (!file.exists()) {
		if (file.mkdir()) {
			System.out.println("Directory is created!");
		} else {
		System.out.println("Failed to create directory!");
		}
	}

        request.setAttribute("insuranceIdProof", insuranceIdProof);
        request.setAttribute("path","VehicleHai/"+activationCode+"/Insurance/"+insuranceIdProof);

        return mapping.findForward(SUCCESS);
    }
}
